﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：角色管理客户端脚本
//创建人：赵国伟
//生成时间：2012/03/18 12:15:00
//----------------------------------------------------------------*/
var ModelOfRole;
var AllRes = null; //权限数据；
var PurviewRoleID = 0;
var AjaxServerUrl = "/Manage/AS/PrivilegeCommon.aspx";
var ModelOfPrivilegeInfo; //权限配置信息；

function Init() {
    PurviewRoleID = 0;
}

function CheckForm() {
    if ($("#txtRoleName").val().trim() == "") {
        alert("提示：请输入角色名称！");
        $("#txtRoleName").focus();
        return false;
    }
    return true;
}


function BindRoleToForm() {
    $("#btnSave").attr("disabled", true);
    var url = AjaxServerUrl+ "?Action=GetRole&roleId=" + PurviewRoleID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (dataInfo) {
            if (dataInfo != null) {
                $("#txtRoleName").val(dataInfo.Role._rolename);
                $("#ddlStatus").val(dataInfo.Role._status);
                if (dataInfo.Privileges != null) {
                    for (var i = 0; i < dataInfo.Privileges.length; ++i) {
                        //设置选中；
                        if ($("#chkResIdsOf" + dataInfo.Privileges[i].ResourceID) != null)
                            $("#chkResIdsOf" + dataInfo.Privileges[i].ResourceID).attr("checked", true);

                        if (dataInfo.Privileges[i].Operates != null) {
                            //操作属性设置；
                            for (var j = 0; j < dataInfo.Privileges[i].Operates.length; ++j) {
                                if ($("#chkOperateOf" + dataInfo.Privileges[i].Operates[j] + "for" + dataInfo.Privileges[i].ResourceID) != null)
                                    $("#chkOperateOf" + dataInfo.Privileges[i].Operates[j] + "for" + dataInfo.Privileges[i].ResourceID).attr("checked", true);
                            }
                        }
                    }
                }
                $("#btnSave").attr("disabled", false);
            }
        }
    });
}

function Save() {
    $("#btnSave").attr("disabled", true);  //将提交按钮置为无效；
    //检测表单数据；
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return false;
    }

    var roleinfo = clone(ModelOfRole);
    roleinfo._purviewroleid = PurviewRoleID;
    roleinfo._rolename = $("#txtRoleName").val().trim();
    roleinfo._status = $("#ddlStatus").val();
    roleinfo._lastmodifydate = "/Date(" + CurrentDateStringToUTC() + ")/";
    roleinfo._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    //获取选中的权限信息；
    var SelectRes = new Array();

    //遍历权限树；获取用户的选中情况；
    if (AllRes != null) {
        
        for (var i = 0; i < AllRes.length; ++i) {
            //检查的当前权限的选中情况
            var resourceId = AllRes[i].Res._purviewresourceid; //菜单资源编号；
            if ($("#chkResIdsOf" + resourceId).attr("checked")) {
                //记录下；
                var PrivilegeInfo = clone(ModelOfPrivilegeInfo);
                PrivilegeInfo.ResourceID = resourceId;
                //检查该权限的操作信息；
                if (AllRes[i].Operates != null) {
                    PrivilegeInfo.Operates = new Array();
                    for (var j = 0; j < AllRes[i].Operates.length; ++j) {
                        if ($("#chkOperateOf" + AllRes[i].Operates[j]._id + "for" + resourceId).attr("checked")) {
                            PrivilegeInfo.Operates.push(AllRes[i].Operates[j]._id);
                        }
                    }
                }
                SelectRes.push(PrivilegeInfo);  //保存当前资源的配置值；
            }
        }
    }
    var postData = "jsonData=" + JSON.stringify(eval(roleinfo));
    postData += "&jsonSelectRes=" + JSON.stringify(eval(SelectRes));

    //开始提交表单数据；
    var url = AjaxServerUrl + "?Action=SaveRole";
    AjaxPostEx(url, "btnSave", postData);
}

function Delete(id, usedCounts) {
    var tips = "确定要删除吗？";
    if (usedCounts > 0)
        tips += "该角色已被" + usedCounts + "个用户使用！";
    if (!confirm(tips))
        return false;
    var url = AjaxServerUrl+ "?Action=DeleteRole&roleId=" + id;
    AjaxGet(url, null, "删除成功！");
}

function Add() {
    Init();
    GetPopupDiv(700, 610, "#e2e2e2", "添加角色", GetRoleHtmlForm());
}


function Edit(id) {
    Init();
    PurviewRoleID = id;
    GetPopupDiv(700, 610, "#e2e2e2", "角色信息编辑", GetRoleHtmlForm());
    BindRoleToForm();
}


function GetRoleHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"700\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td width=\"70\" align=\"right\" bgcolor=\"#FFFFFF\">角色名称：</td>");
    html.push("    <td width=\"630\" bgcolor=\"#FFFFFF\">");
    html.push("<input name=\"txtRoleName\" type=\"text\" class=\"border2\" id=\"txtRoleName\" value=\"\" size=\"40\">");
    html.push("      * </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\" bgcolor=\"#FFFFFF\">角色状态：</td>");
    html.push("    <td bgcolor=\"#FFFFFF\">");
    html.push("<select name=\"ddlStatus\" id=\"ddlStatus\">");
    html.push("        <option value=\"正用\">正用</option>");
    html.push("        <option value=\"停用\">停用</option>");
    html.push("      </select></td>");
    html.push("  </tr>");


    html.push("  <tr>");
    html.push("    <td align=\"right\" valign=\"top\" bgcolor=\"#FFFFFF\"><br>权限分配：</td>");
    html.push("    <td bgcolor=\"#FFFFFF\">");
    //=======权限分配操作部分===========================================================================

    html.push("<div style=\"width:auto;height:360px;overflow:auto;border: 2px  inset #FFFFFF;\">");
    html.push("<table width=\"600\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"table\" >");

    if (AllRes != null) {

        for (var i = 0; i < AllRes.length; ++i) {
            var resourceId = AllRes[i].Res._purviewresourceid; //菜单资源编号；
            if (i == 0 || (i > 0 && AllRes[i].GroupInfo._name != AllRes[i - 1].GroupInfo._name)) {
                //输出组单元行； 
                html.push("      <tr>");
                html.push("        <td colspan=\"2\" class=\"green bold\">" + AllRes[i].GroupInfo._name + "</td>");
                html.push("        </tr>");
            }
            html.push("      <tr onmouseover=\"this.style.backgroundColor='#E6F5FA';\" onmouseout=\"this.style.backgroundColor='#FFFFFF'\" bgcolor=\"#FFFFFF\">");
            html.push("        <td width=\"150\" >");

            //输出资源名称；
            html.push("<label for=\"chkResIdsOf" + AllRes[i].Res._purviewresourceid + "\" >");
            html.push("<input type=\"checkbox\" id=\"chkResIdsOf" + AllRes[i].Res._purviewresourceid + "\" name=\"chkResIds\" value=\"" + AllRes[i].Res._purviewresourceid + "\" /><img src=\"/Manage/images/arrow-item.gif\"  /> ");
            html.push(AllRes[i].Res._resourcename);
            html.push("</label>");
            html.push("</td>");
            html.push("        <td  >");
            if (AllRes[i].Operates != null) {

                for (var j = 0; j < AllRes[i].Operates.length; ++j) {
                    html.push("<label for=\"chkOperateOf" + AllRes[i].Operates[j]._id + "for" + AllRes[i].Res._purviewresourceid + "\">");
                    html.push("<input type=\"checkbox\" name=\"chkOperate\" id=\"chkOperateOf" + AllRes[i].Operates[j]._id + "for" + AllRes[i].Res._purviewresourceid + "\" value=\"" + AllRes[i].Operates[j]._id + "\" />");
                    html.push(AllRes[i].Operates[j]._name);
                    html.push("</label>");
                    html.push("&nbsp;");
                    if ((j + 1) % 5 == 0)
                        html.push("<br>");
                }
            }
            html.push("            </td>");
            html.push("      </tr>");

        }
    }
    html.push("    </table>");
    html.push(" </div>");
    //=======权限分配操作部分结束=======================================================================
    html.push("      </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" align=\"center\" bgcolor=\"#FFFFFF\">");
    html.push("      <input type=\"button\" name=\"btnSave\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("        <input type=\"button\" name=\"btnCloseForm\" value=\"关闭窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");
}





